package com.power.web.action.hospital;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.power.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.power.Constants;
import com.power.model.hospital.TiHospital;
import com.power.model.user.TsSysuser;
import com.power.services.hospital.HospitalService;
import com.power.util.DateTimeUtil;
import com.power.util.PingYinUtil;
import com.power.util.RequestUtil;
import com.power.util.XmlUtil;
import com.power.web.action.base.AbstractBaseAction;

public class TiHospitalAction extends AbstractBaseAction {

	private static final long serialVersionUID = 4628301066492657392L;
	private String jsonString;
	@Autowired
	private HospitalService service;
	private int flag;
	private TiHospital hospital;
	private File imgFile;
	private String imgFileFileName;
    private File videoFile;
    private String videoFileFileName;
	
	
	public String showPage() {
		Integer hid = RequestUtil.getParam(request, "hid", 0);
		try {
			if (hid == 0) {
				hospital = new TiHospital();
				hospital.setDeleteFlag("0");
			} else {
				hospital = service.read(hid);
				hospital.setProviceId(service.getProId(hospital.getDistrictId()));
				hospital.setProviceName(service.getProName(hospital.getDistrictId()));
				hospital.setDistrictName(service.getDistrictName(hospital.getDistrictId()));
			}
		} catch (Exception e) {
			log.error("获取数据错误");
			e.printStackTrace();
		}
		return "showPage";
	}
	
	public String save() {
		this.response.reset();
		this.response.setCharacterEncoding("UTF-8");
			if (null != hospital) {
				if (null != imgFile) {
					String basePath = request.getSession().getServletContext()
							.getRealPath("/");
					String compressPath = XmlUtil.getValue("imgPath",
							"sysConfig")
							+ "/";
					String fileName = "";
					fileName = new Date().getTime()
							+ imgFileFileName.substring(imgFileFileName
									.lastIndexOf("."));
					if (FileUtil.doUpFile(basePath + compressPath + fileName,
							imgFile)) {
						hospital.setImgsrc(compressPath + fileName);
					}
				}
					if (null != videoFile) {
					    String    basePath = request.getSession().getServletContext()
                                .getRealPath("/");
					    String  compressPath = XmlUtil.getValue("imgPath",
                                "sysConfig")
                                + "/";
					    String   fileName = new Date().getTime()
                                + videoFileFileName.substring(videoFileFileName
                                        .lastIndexOf("."));
                        if (FileUtil.doUpFile(basePath + compressPath + fileName,
                            videoFile)) {
                            hospital.setVideosrc(compressPath + fileName);
                        }
					}
	                
				
				TsSysuser user = (TsSysuser) this.session
						.get(Constants.USERINFO_KEY);
				hospital.setInputDate(DateTimeUtil.getCurrentDateTime());
				hospital.setInputMan(user.getUsername());
				hospital.setInputManId(user.getId());
				hospital.setFullChar(PingYinUtil.getPingYin(hospital.getName(), 1, 1));
				hospital.setFirstChar(PingYinUtil.getPingYin(hospital.getName(), 2, 1).substring(0,1));
			}
			try {
				service.saveOrUpdate(hospital);
				request.setAttribute("ifsuccess", "保存成功!");
			} catch (RuntimeException e) {
				request.setAttribute("ifsuccess", "保存失败!");
				e.printStackTrace();
			}
		return "ifsuccess";
	}

	public String delete() {
		this.response.reset();
		this.response.setCharacterEncoding("UTF-8");
		String ids = RequestUtil.getParam(request, "deleteID", "");
		try {
			service.deleteHos(ids);
			this.response.getWriter().print("{success:true,msg:'删除成功!'}");
		} catch (Exception e) {
			try {
				if ("1".equals(e.getMessage())) {
					this.response.getWriter().print(
							"{success:false,msg:'请先删除省级下面的市级信息,删除失败!'}");
				} else {
					log.error("地区保存失败", e);
					this.response.getWriter().print(
							"{success:false,msg:'删除失败!'}");
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}


	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public TiHospital getHospital() {
		return hospital;
	}

	public void setHospital(TiHospital hospital) {
		this.hospital = hospital;
	}

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

    public File getVideoFile() {
        return videoFile;
    }

    public void setVideoFile(File videoFile) {
        this.videoFile = videoFile;
    }

    public String getVideoFileFileName() {
        return videoFileFileName;
    }

    public void setVideoFileFileName(String videoFileFileName) {
        this.videoFileFileName = videoFileFileName;
    }

}
